package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.EduService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class EduController {
    @Autowired
    EduService eduService;
    //# 1. 流量分时统计sql  1 小时  独立访客数
    @RequestMapping("/hourCount")
    public Object hourCount(@RequestParam("nowDate") String  nowDate){
        List<Map> list = eduService.hourCount(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray catArray = new JSONArray();
        for (Map map : list) {
            catArray.add(map.get("h"));
        }
        dataObject.put("categories",catArray);

        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        serObject.put("name","独立访客数");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("total"));
        }
        serObject.put("data",dataArray);
        serArray.add(serObject);
        dataObject.put("series",serArray);

        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //# 2. 各来源交易统计sql  来源 订单总额 下单人数 订单数
    @RequestMapping("/orderTotal")
    public Object orderTotal(@RequestParam("nowDate") String  nowDate){
        List<Map> list = eduService.orderTotal(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colArray = new JSONArray();

        JSONObject colObject = new JSONObject();
        colObject.put("name","来源名称");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","订单总额");
        colObject1.put("id","module");
        colArray.add(colObject1);

        JSONObject colObject2 = new JSONObject();
        colObject2.put("name","下单的用户数");
        colObject2.put("id","business");
        colArray.add(colObject2);

        JSONObject colObject3 = new JSONObject();
        colObject3.put("name","订单总数");
        colObject3.put("id","name");
        colArray.add(colObject3);

        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("NAME"));
            rowObject.put("module",map.get("total"));
            rowObject.put("business",map.get("orderPel"));
            rowObject.put("name",map.get("num"));
            rowArray.add(rowObject);
        }
        dataObject.put("columns",colArray);
        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //    # 3.  新增交易用户统计sql  新增下单人数
    @RequestMapping("/newPeople")
    public Object  newPeople(@RequestParam("nowDate") String  nowDate){
        List<Map> list = eduService.newPeople(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",list.get(0).get("num"));
        return jsonObject;
    };
    //    # 课程	下单金额
    @RequestMapping("/couMoney")
    public Object  couMoney(@RequestParam("nowDate") String  nowDate){
        List<Map> list = eduService.couMoney(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colArray = new JSONArray();
        JSONObject colObject = new JSONObject();
        colObject.put("name","课程名称");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","订单总额");
        colObject1.put("id","module");
        colArray.add(colObject1);


        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("NAME"));
            rowObject.put("module",map.get("total"));
            rowArray.add(rowObject);
        }
        dataObject.put("columns",colArray);
        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //    # 学科	下单金额
    @RequestMapping("/subMoney")
    public Object  subMoney(@RequestParam("nowDate") String  nowDate){
        List<Map> list = eduService.subMoney(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colArray = new JSONArray();
        JSONObject colObject = new JSONObject();
        colObject.put("name","学科名称");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","订单总额");
        colObject1.put("id","module");
        colArray.add(colObject1);


        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("NAME"));
            rowObject.put("module",map.get("total"));
            rowArray.add(rowObject);
        }
        dataObject.put("columns",colArray);
        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //    # 类别	下单金额
    @RequestMapping("/catMoney")
    public Object catMoney(@RequestParam("nowDate") String  nowDate){
        List<Map> list = eduService.catMoney(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colArray = new JSONArray();
        JSONObject colObject = new JSONObject();
        colObject.put("name","类别名称");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","订单总额");
        colObject1.put("id","module");
        colArray.add(colObject1);


        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("NAME"));
            rowObject.put("module",map.get("total"));
            rowArray.add(rowObject);
        }
        dataObject.put("columns",colArray);
        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
}
